home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 May / PCPlus May 1998=disk A.iso / full / CBUILDER / SAMS / SAMPLES / CHAP03 / POINTER.CPP next >
Encoding:
C/C++ Source or Header  |  1997-02-12  |  2.3 KB  |  101 lines

  1. #include <iostream.h>
  2. #include <iostream.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #pragma hdrstop
  6.  
  7. #include "structur.h"
  8.  
  9. void displayRecord(int, mailingListRecord mlRec);
  10.  
  11. int main(int, char**)
  12. {
  13.   //
  14.   // create an array of pointers to
  15.   // the mailingListRecord structure
  16.   //
  17.   mailingListRecord* listArray[3];
  18.   //
  19.   // create an object for each element of the array
  20.   //
  21.   for (int i=0;i<3;i++)
  22.     listArray[i] = new mailingListRecord;
  23.   cout << endl;
  24.   int index = 0;
  25.   //
  26.   // get three records
  27.   //
  28.   do {
  29.     cout << "First Name: ";
  30.     cin.getline(listArray[index]->firstName,
  31.       sizeof(listArray[index]->firstName) - 1);
  32.     cout << "Last Name: ";
  33.     cin.getline(listArray[index]->lastName,
  34.       sizeof(listArray[index]->lastName) - 1);
  35.     cout << "Address: ";
  36.     cin.getline(listArray[index]->address,
  37.       sizeof(listArray[index]->address) - 1);
  38.     cout << "City: ";
  39.     cin.getline(listArray[index]->city,
  40.       sizeof(listArray[index]->city) - 1);
  41.     cout << "State: ";
  42.     cin.getline(listArray[index]->state,
  43.       sizeof(listArray[index]->state) - 1);
  44.     char buff[10];
  45.     cout << "Zip: ";
  46.     cin.getline(buff, sizeof(buff) - 1);
  47.     listArray[index]->zip = atoi(buff);
  48.     index++;
  49.     cout << endl;
  50.   }
  51.   while (index < 3);
  52.   //
  53.   // display the three records
  54.   //
  55.   clrscr();
  56.   //
  57.   // must dereference the pointer to pass an object
  58.   // to the displayRecord function.
  59.   //
  60.   for (int i=0;i<3;i++) {
  61.     displayRecord(i, *listArray[i]);
  62.   }
  63.   //
  64.   // ask the user to choose a record
  65.   //
  66.   cout << "Choose a record: ";
  67.   char rec;
  68. do {
  69.     rec = getch();
  70.     rec -= 49;
  71.   } while (rec < 0 || rec > 2);
  72.   //
  73.   // assign the selected record to a temporary variable
  74.   // must dereference here, too
  75.   //
  76.   mailingListRecord temp = *listArray[rec];
  77.   clrscr();
  78.   cout << endl;
  79.   //
  80.   // display the selected recrord
  81.   //
  82.   displayRecord(rec, temp);
  83.   getch();
  84.   return 0;
  85. }
  86.  
  87. void displayRecord(int num, mailingListRecord mlRec)
  88. {
  89.   cout << "Record " << num + 1 << ":" << endl;
  90.   cout << "Name:     " << mlRec.firstName << " ";
  91.   cout << mlRec.lastName;
  92.   cout << endl;
  93.   cout << "Address:  " << mlRec.address;
  94.   cout << endl << "          ";
  95.   cout << mlRec.city << ", ";
  96.   cout << mlRec.state << "  ";
  97.   cout << mlRec.zip;
  98.   cout << endl << endl;
  99. }
  100.  
  101.